################################################################################
##
## Purpose: This script creates Figure 6, along with SI table 5
##
## Author: James Bisbee (james.h.bisbee@vanderbilt.edu)
##
## Input Files:
##  - ./data/prepped/finalData.RData: Prepped data from 9_DATA_final_build.R
##
## Output Files:
##  - ./output/figures/MS_figure_6.pdf
##  - ./output/tables/SI_table_5.tex
##
##
## See associated log file for compute environment, package versions, 
##  and date of most recent run.
##
################################################################################
rm(list = ls())
gc()
require(tidyverse)
require(ggridges)
require(fixest)
require(ggrepel)
require(marginaleffects)

set.seed(123)

# Compute details
print(paste0('Compute environment from ',Sys.Date(),' run by Bisbee'))
if(Sys.info()['sysname'] == 'Windows') {
  ram_size = system("wmic MemoryChip get Capacity", intern = TRUE)[-1]
  model_name = system("wmic cpu get name", intern = TRUE)[2] # nocov
  vendor_id = system("wmic cpu get manufacturer", intern = TRUE)[2] # nocov
  
  print(list(ram = stringr::str_squish(ram_size)[1],
             vendor_id = stringr::str_squish(vendor_id),
             model_name = stringr::str_squish(model_name),
             no_of_cores = parallel::detectCores()))
} else if(Sys.info()['sysname'] == 'Linuxs') {
  splitted <- strsplit(system("ps -C rsession -o %cpu,%mem,pid,cmd", intern = TRUE), " ")
  df <- do.call(rbind, lapply(splitted[-1], 
                              function(x) data.frame(
                                cpu = as.numeric(x[2]),
                                mem = as.numeric(x[4]),
                                pid = as.numeric(x[5]),
                                cmd = paste(x[-c(1:5)], collapse = " "))))
  df
} else {
  cat("If not on Linux or Windows, you'll have to figure out your own solution to seeing the compute environment.")
}

sessionInfo()


load('./data/prepped/finalData.RData')



# Rigorous fixed effects
dyadToAnal <- utterance_level %>%
  mutate_at(vars(matches('lag')),function(x) ifelse(is.na(x),0,x)) %>%
  mutate(votepct_rel = ifelse(is.infinite(votepct_rel),1,votepct_rel),
         respondingTo = relevel(factor(respondingTo),ref = 'FEDBERNANKE'))

dims <- colnames(utterance_level %>% select(matches('SENT_'),-matches('_lag|error')) %>% select(-matches('SEVERE|AUTHOR|LIKELY')))

# diff-in-diff
summary(mod <- feols(as.formula(paste0('interruptor ~ fedResp*yellenTime + poly(year,3) + ',
                                       'poly(tot_utterances_log,3) + poly(nchars_lag_log,3) + ',
                                      paste(paste0('topic_',1:100,'_lag'),collapse = ' + '),
                                      ' + ',
                                      paste(paste0('scale(',dims[which(grepl('comb',dims))],'_lag)'),collapse = ' + '),
                                      '| opensecretsID + chamber')),
                    utterance_level %>%
                      mutate(yellenTime = ifelse(yellenTime,1,0),
                             tot_utterances_log = log(tot_utterances),
                             clust = paste0(opensecretsID,docID),
                             nchars_lag_log = log(nchars_lag)) %>%
                      filter(ind > mind),
                    cluster = c('clust')))


summary(mod)
toplot <- plot_cme(mod,variables = 'fedResp',condition = 'yellenTime',draw = F)

toadd <- toplot %>%
  select(estimate,std.error) %>%
  summarise(diff = diff(estimate),
            se = sqrt(sum(std.error^2)))

toplot <- toplot %>%
  bind_cols(toadd)

pFull <- toplot %>%
  ggplot(aes(x = factor(yellenTime),y = estimate,group = term)) + 
  geom_point() + 
  geom_line() + 
  geom_errorbar(aes(ymin = conf.low,ymax = conf.high),width = 0) + 
  theme_ridges() + 
  scale_x_discrete(labels = c('Male Fed Chairs','Yellen')) + 
  geom_hline(yintercept = 0,linetype = 'dashed') + 
  geom_label(data = toplot %>%
               filter(yellenTime == 1),x = 1.5,
             aes(y = estimate - diff/2,label = paste0(round(diff,3),' (',round(se,3),')')),
             hjust = .5) + 
  labs(x = NULL,y = 'Increase in Interruptions\nAssociated with Fed Chair')


# Moderator by Party
summary(modP <- feols(as.formula(paste0('interruptor ~ fedResp*yellenTime*party + poly(year,3) + ',
                                       'poly(tot_utterances_log,3) + poly(nchars_lag_log,3) + ',
                                       paste(paste0('topic_',1:100,'_lag'),collapse = ' + '),
                                       ' + ',
                                       paste(paste0('scale(',dims[which(grepl('comb',dims))],'_lag)'),collapse = ' + '),
                                       '| opensecretsID + chamber')),
                     utterance_level %>%
                       mutate(yellenTime = ifelse(yellenTime,1,0),
                              tot_utterances_log = log(tot_utterances),
                              clust = paste0(opensecretsID,docID),
                              nchars_lag_log = log(nchars_lag)) %>%
                       filter(ind > mind,
                              party %in% c('D','R')),
                     cluster = c('clust')))

toplotP <- plot_cme(modP,variables = 'fedResp',condition = (c('yellenTime','party')),draw = F)

toplotP <- toplotP %>%
  left_join(toplotP %>%
  group_by(party) %>%
  summarise(diff = diff(estimate),
            se = sqrt(sum(std.error^2))) %>%
    ungroup() %>%
    mutate(pval = 2*pt(diff/se,df = 1000,lower.tail = F),
           stars = ifelse(pval < .001,'***',ifelse(pval < .01,'**',
                                                   ifelse(pval < .05,'*','')))))



pParty <- toplotP %>%
  ggplot(aes(x = factor(yellenTime),y = estimate,group = party,color = party,shape = party)) + 
  geom_point(position = position_dodge(width = 0)) + 
  geom_errorbar(aes(ymin = conf.low,ymax = conf.high),width = 0,position = position_dodge(width = 0)) + 
  geom_line(position = position_dodge(width = 0)) + 
  theme_ridges() + 
  scale_x_discrete(labels = c('Male Chairs','Yellen'),expand = c(.1,.1,.1,.7)) + 
  geom_hline(yintercept = 0,linetype = 'dashed') + 
  geom_label(data = toplotP %>%
               filter(yellenTime == 1),
             aes(y = estimate,label = paste0(party,': ',round(diff,3),'\n     (',round(se,2),')')),
             hjust = -.1) + 
  scale_color_manual(guide = 'none',name = 'Party',values = c('blue','red')) + 
  scale_shape_discrete(guide = 'none',name = 'Party') + 
  labs(x = NULL,y = NULL,
       subtitle = 'By Party')

pParty

summary(modG <- feols(as.formula(paste0('interruptor ~ fedResp*yellenTime*gender + poly(year,3) + ',
                                       'poly(tot_utterances_log,3) + poly(nchars_lag_log,3) + ',
                                       paste(paste0('topic_',1:100,'_lag'),collapse = ' + '),
                                       ' + ',
                                       paste(paste0('scale(',dims[which(grepl('comb',dims))],'_lag)'),collapse = ' + '),
                                       '| opensecretsID + chamber')),
                     utterance_level %>%
                       mutate(yellenTime = ifelse(yellenTime,1,0),
                              tot_utterances_log = log(tot_utterances),
                              clust = paste0(opensecretsID,docID),
                              nchars_lag_log = log(nchars_lag)) %>%
                       filter(ind > mind),
                     cluster = c('clust')))

toplotG <- plot_cme(modG,variables = 'fedResp',condition = (c('yellenTime','gender')),draw = F)

toplotG <- toplotG %>%
  left_join(toplotG %>%
              group_by(gender) %>%
              summarise(diff = diff(estimate),
                        se = sqrt(sum(std.error^2))) %>%
              ungroup() %>%
              mutate(pval = 2*pt(diff/se,df = 1000,lower.tail = F),
                     stars = ifelse(pval < .001,'***',ifelse(pval < .01,'**',
                                                             ifelse(pval < .05,'*','')))))

pGender <- toplotG %>%
  ggplot(aes(x = factor(yellenTime),y = estimate,group = gender,color = gender,shape = gender)) + 
  geom_point(position = position_dodge(width = 0)) + 
  geom_errorbar(aes(ymin = conf.low,ymax = conf.high),width = 0,position = position_dodge(width = 0)) + 
  geom_line(position = position_dodge(width = 0)) + 
  theme_ridges() + 
  scale_x_discrete(labels = c('Male Chairs','Yellen'),expand = c(.1,.1,.1,.7)) + 
  geom_hline(yintercept = 0,linetype = 'dashed') + 
  geom_label_repel(data = toplotG %>%
               filter(yellenTime == 1),direction = 'y',min.segment.length = 100,nudge_x = .1,
             aes(y = estimate,label = paste0(gender,': ',round(diff,3),'\n     (',round(se,2),')'))) + 
  scale_color_manual(guide = 'none',name = 'Party',values = c('darkgreen','tomato')) + 
  scale_shape_discrete(guide = 'none',name = 'Party') + 
  labs(x = NULL,y = NULL,
       subtitle = 'By Gender')


modG$coeftable['fedResp:yellenTime',1] - modG$coeftable[1,1]
modG$coeftable['fedResp',1]
sum(modG$coeftable[c('fedResp','fedResp:genderM'),1])
sum(modG$coeftable[c('fedResp','fedResp:yellenTime'),1])
sum(modG$coeftable[c('fedResp','fedResp:genderM','fedResp:yellenTime','fedResp:yellenTime:genderM'),1])


sum(modG$coeftable[c(1,2,3),1])
(modG$coeftable[1,1] + modG$coeftable['fedResp:genderM',1] + modG$coeftable['yellenTime:genderM',1] + 
    modG$coeftable['fedResp:yellenTime:genderM',1] + modG$coeftable['fedResp:yellenTime',1]) - 
    (modG$coeftable[1,1] + modG$coeftable['fedResp:genderM',1])



require(patchwork)
pdf('./output/figures/MS_figure_6.pdf',width = 7,height = 5)
(pFull & labs(subtitle = 'Overall DiD')) + ((pParty & theme(axis.text.x = element_blank())) / pGender) +
  plot_layout(widths = c(4,2))
dev.off()

etable(mod,modP,modG,
       extralines = list('100 LDA Topic Loadings' = c('Yes','Yes','Yes')),
       drop = 'opensecretsID\\)N|topic',replace = T,headers = c('Overall DiD','By Party','By Gender'),
       file = './output/tables/SI_table_5.tex')

#EOF